package com.aacar.controller.common;

import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.aacar.dto.DataDto;
import com.aacar.dto.Header;
import com.aacar.dto.VersionDto;
import com.aacar.service.VesionService;
import com.aacar.utils.ResultInfoUtil;
import com.aacar.vo.DataVersionVo;
import com.aacar.vo.VersionUrlVo;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

/**
 * 版本管理
 * @author wangh
 *
 */
@RestController
@RequestMapping("/version")
public class VesionController {
	
	private Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private VesionService vesionService;
	
	//判断版本是否需要更新
	@RequestMapping(value="/compareAppVersion/0", method = RequestMethod.POST)
	@ResponseBody
//	@MemLoginKeyValid
	public Object compareAppVersion(@RequestHeader(value="mobilehead") String mobilehead) {
		logger.info("request:/coupon/compareAppVersion/0" + mobilehead);
		Header header = null;
		if (null != mobilehead) {
			header = JSON.parseObject(mobilehead, Header.class);
		}
		//1.传值
		VersionDto vdt = new VersionDto();
		vdt.setVersion(header.getVersion());
		vdt.setAppType(header.getDeviceType());//应用名字andriod-type ios-type
		VersionUrlVo vo = this.vesionService.getNewVersion(vdt);
		//比较版本
		return ResultInfoUtil.setSuccessInfo(vo, header.getService());
	}
	
	//检测数据版本
	@RequestMapping(value="/compareDataVersion/0", method = RequestMethod.POST)
	@ResponseBody
	public Object compareDataVersion(@RequestHeader(value="mobilehead") String mobilehead,@RequestBody DataDto dataDto) {
		logger.info("request:/coupon/compareDataVersion/0" +  JSONObject.toJSONString(dataDto));
		Header header = null;
		if (null != mobilehead) {
			header = JSON.parseObject(mobilehead, Header.class);
		}
		Map<String,Object> result = this.vesionService.checkDataVersion(dataDto);
		return ResultInfoUtil.setSuccessInfo(result, header.getService());
	}
	
	//获取数据初始版本
	@RequestMapping(value="/getDataVersion/0", method = RequestMethod.POST)
	@ResponseBody
	public Object getDataVersion(@RequestHeader(value="mobilehead") String mobilehead,@RequestBody DataDto dataDto) {
		logger.info("request:/coupon/getDataVersion/0" +  JSONObject.toJSONString(dataDto));
		Header header = null;
		if (null != mobilehead) {
			header = JSON.parseObject(mobilehead, Header.class);
		}
		DataVersionVo result = this.vesionService.getDataVersion(dataDto);
		return ResultInfoUtil.setSuccessInfo(result, header.getService());
	}
	
}
